package com.hoopchina.ui.main;

import com.hoopchina.news.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

/**
 * TabBarButton
 * TabBar上显示的按钮
 * 
 * @author Neusoft
 */
public class TabBarButton extends RelativeLayout {
	private final int               mNormalBackgroundColor = 0x00000000;
	private int                     mFocusedBackgroundImageResource;
	private int                     mNormalBackgroundImageResource;
	private int                     mNormalImageResource;
	private int                     mFocusedImageResource;
	private int                     mPressedImageResource;
	private int                     mNormalTextColor;
	private int                     mFocusedTextColor;
	private Button                  mButton;
	private int                     oldres;   
//	private ImageView               mImage;
//	private ImageView               mDecorIcon;
//	private TextView                mDecorText;
//	private RelativeLayout          mDecorBox;
	private boolean                 mAnimEnabled;
	OnTouchListener               lister1 = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN )
            {
            	mButton.setBackgroundResource(mPressedImageResource);
            }
            else if( event.getAction() == MotionEvent.ACTION_UP )
            {
            	mButton.setBackgroundResource(oldres);
            }
            	
			return false;
		}
		
	};

	/*
	 * 构造器
	 */
	public TabBarButton(Context context) {
		super(context);
		
		LayoutInflater inflater = 
			( LayoutInflater )getContext()
			.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		inflater.inflate( R.layout.tabbarbutton, this, true );
		
		mButton = ( Button )findViewById( R.id.btnTabBarButton );
	//	mImage = ( ImageView )findViewById( R.id.imgTabBarButton );
//		mDecorIcon = ( ImageView )findViewById( R.id.imgTBButtonDecor );
//		mDecorText = ( TextView )findViewById( R.id.txtTBButtonDecor );
//		mDecorBox = ( RelativeLayout)findViewById( R.id.boxTabBarButtonDecor );
//		mDecorBox.setVisibility( View.GONE );
	}
	
	public void setAnimEnabled( boolean aEnabled ) {
		mAnimEnabled = aEnabled;
	}
	
	/**
	 * 设置按钮的属性
	 * @param aNormalImageResource 原始背景
	 * @param aFocusedImageResource 选中后背景
	 * @param aListener  监听器
	 */
	public void setButtonProperties( 
			int aNormalImageResource,
			int aFocusedImageResource,
			int aPressedImageResource,
			OnClickListener aListener ) {
		mNormalImageResource = aNormalImageResource;
		mFocusedImageResource = aFocusedImageResource;
		mPressedImageResource = aPressedImageResource;
		mButton.setOnClickListener( aListener );
		mButton.setOnTouchListener(lister1);
	}
	
	/**
	 * 设置按钮的文本和文本颜色
	 * @param aText   设置的文本
	 * @param aNormalTextColor 原始颜色
	 * @param aFocusedTextColor 选中后颜色
	 */
	public void setButtonText(
			String aText,
			int aNormalTextColor,
			int aFocusedTextColor ) {
		mButton.setText( aText );
		mNormalTextColor = aNormalTextColor;
		mFocusedTextColor = aFocusedTextColor;
	}
	
	/**
	 * 设置按钮背景资源
	 * @param aNormalBackgroundImageResource 原始图片背景
	 * @param aFocusedBackgroundImageResource 选中后背景
	 */
	public void setBackground( 
			int aNormalBackgroundImageResource, 
			int aFocusedBackgroundImageResource ) {
		mNormalBackgroundImageResource = aNormalBackgroundImageResource;
		mFocusedBackgroundImageResource = aFocusedBackgroundImageResource;
	}
	
	/**
	 * Set button decoration icon
	 * @param aImageSource
	 */
	public void setDecorIcon( int aImageSource, String aMessage ) {
//		mDecorIcon.setBackgroundResource( aImageSource );
//		mDecorText.setText( aMessage );
//		mDecorBox.setVisibility( View.VISIBLE );
	}
	
	/**
	 * Hide button decoration icon
	 */
	public void hideDecorIcon() {
//		mDecorBox.setVisibility( View.GONE );
	}
	
	/**
	 * 设置按钮选中后效果
	 * @param aFocused 是否选中
	 */
	public void setFocused( boolean aFocused ) {
		if ( aFocused ) {
			if ( !mAnimEnabled ) {
				this.setBackgroundResource( mFocusedBackgroundImageResource );
			}
			mButton.setBackgroundResource( mFocusedImageResource );
			oldres = mFocusedImageResource;
	        mButton.setTextColor( mFocusedTextColor );
		}
		else {
			if ( !mAnimEnabled ) {
				if ( mNormalBackgroundImageResource != 0 ) {
					this.setBackgroundResource( mNormalBackgroundImageResource );
				}
				else {
					this.setBackgroundColor( mNormalBackgroundColor );
				}
			}
			mButton.setBackgroundResource( mNormalImageResource );
			oldres = mNormalImageResource;
	        mButton.setTextColor( mNormalTextColor );
		}
	}
	
//	public void setImageSize( int aSize ) {
//		RelativeLayout.LayoutParams layout 
//		= ( RelativeLayout.LayoutParams )mImage.getLayoutParams();
//		layout.addRule( RelativeLayout.CENTER_HORIZONTAL );
//		layout.width = aSize;
//		layout.height = aSize;
//		mImage.setLayoutParams( layout );
//	}
//	
//	public void setChildLayoutParams() {
//		RelativeLayout.LayoutParams layout 
//			= ( RelativeLayout.LayoutParams )mImage.getLayoutParams();
//		layout.addRule( RelativeLayout.CENTER_HORIZONTAL );
//		mImage.setLayoutParams( layout );
//	}
    /*
     * 构造器
     */
	public TabBarButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	/*
	 * 构造器
	 */
	public TabBarButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

}
